package net.chrone.query.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;

public class PropertiesUtil extends PropertyPlaceholderConfigurer{
		private static Map<String, String> propertiesMap;  
	    // Default as in PropertyPlaceholderConfigurer  
	    private int springSystemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK;  
		private Resource log4jProperties;

		// 加载log4j配置文件
		public void log4jInit() throws IOException {
			System.out.println(" System 加载log4j配置文件");
			logger.info("加载log4j配置文件");
			PropertyConfigurator.configure(log4jProperties.getInputStream());
			System.out.println(" System log4j配置文件加载完毕");
			logger.info("log4j配置文件加载完毕");
		}  
	    
	    
	    @Override  
	    public void setSystemPropertiesMode(int systemPropertiesMode) {  
	        super.setSystemPropertiesMode(systemPropertiesMode);  
	        springSystemPropertiesMode = systemPropertiesMode;  
	    }  
	
	  @Override  
	    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {  
	        super.processProperties(beanFactory, props);  
	        propertiesMap = new HashMap<String, String>();  
	        for (Object key : props.keySet()) {  
	            String keyStr = key.toString();  
	            String valueStr = resolvePlaceholder(keyStr, props, springSystemPropertiesMode);  
	            propertiesMap.put(keyStr, valueStr);  
	        }  
	    }  

	public Resource getLog4jProperties() {
		return log4jProperties;
	}


	public void setLog4jProperties(Resource log4jProperties) {
		this.log4jProperties = log4jProperties;
	}


		public static String getProperty(String name) {  
	        return propertiesMap.get(name).toString();  
	    }  
}
